using UnityEngine;

[ExecuteInEditMode]
public class ControllerBulletBallSpawner : ControllerGeneric
{
	private Vector3 fixedScale = new Vector3(1f, 1f, 1f);

	public GameObject bulletBall;

	public float bulletSpeed = 1f;

	public float bulletRange = 200f;

	public float delay;

	public float interval = 1f;

	public float intervalVariation;

	public GameObject startClipPlane;

	public GameObject endClipPlane;

	private bool isActive;

	private bool init = true;

	private float startTime;

	private bool running;

	private float lastIntervalTime;

	private float nextDuration;

	public override void OnLevelReady(Transform levelTransform)
	{
		base.OnLevelReady(levelTransform);
		if (levelTransform == base.transform.root)
		{
			isActive = true;
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = fixedScale;
				remoteControl.scale = fixedScale;
			}
			Setup();
		}
	}

	private void FixedUpdate()
	{
		if (isActive)
		{
			if (init)
			{
				startTime = Time.time;
				init = false;
			}
			if (!running && Time.time - startTime - delay >= (0f - Time.fixedDeltaTime) / 2f)
			{
				running = true;
				lastIntervalTime = Time.time;
				nextDuration = interval + ((!(intervalVariation > 0f)) ? 0f : (Random.Range(0f, intervalVariation * 2f) - intervalVariation));
			}
			if (running && Time.time - lastIntervalTime - nextDuration >= (0f - Time.fixedDeltaTime) / 2f)
			{
				Shot();
				lastIntervalTime += interval;
				nextDuration = interval + ((!(intervalVariation > 0f)) ? 0f : (Random.Range(0f, intervalVariation * 2f) - intervalVariation));
			}
		}
	}

	private void Shot()
	{
		Vector3 position = base.transform.position;
		Vector3 end_ = base.transform.position + base.transform.forward * bulletRange;
		GameObject gameObject = Object.Instantiate(bulletBall) as GameObject;
		gameObject.transform.parent = base.transform;
		BulletBall component = gameObject.GetComponent<BulletBall>();
		if (startClipPlane != null)
		{
			float x = startClipPlane.transform.up.x;
			float y = startClipPlane.transform.up.y;
			float z = startClipPlane.transform.up.z;
			float w = 0f - Vector3.Dot(startClipPlane.transform.up, startClipPlane.transform.position);
			component.startClip = true;
			component.startClipPlaneCoef = new Vector4(x, y, z, w);
		}
		component.Shot(position, end_, bulletSpeed);
	}

	private void Setup()
	{
		AssureParenting();
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = fixedScale;
			remoteControl.gameObject.AddComponent<PresetsBulletBallSpawner>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerBulletBallSpawner component = rc.controller.GetComponent<ControllerBulletBallSpawner>();
		bulletSpeed = component.bulletSpeed;
		delay = component.delay;
		interval = component.interval;
		intervalVariation = component.intervalVariation;
		PresetsBulletBallSpawner component2 = remoteControl.gameObject.GetComponent<PresetsBulletBallSpawner>();
		component2.bulletSpeed = component.bulletSpeed;
		component2.delay = component.delay;
		component2.interval = component.interval;
		component2.intervalVariation = component.intervalVariation;
	}
}
